package com.evil.application.listener;

import com.evil.Application;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @author liyang
 * @date 2023-05-16 14:16
 */
@Slf4j
@Component
@RocketMQMessageListener(
        topic = "test",
        consumerGroup = "test-group-1"
)
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class ConsumerTestA implements RocketMQListener<String> {

    @Resource
    private RocketMQTemplate rocketMQTemplate;
    @Resource
    private Environment environment;

    @Override
    public void onMessage(String message) {
        String port = environment.getProperty("server.port");
        log.info("test consumer port {} | {}", port, message);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void main() throws InterruptedException {
        for (int i = 0; i < 1000; i++) {
            rocketMQTemplate.convertAndSend("test", "ConsumerTestA" + i);
        }
    }
}
